<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/css.css" rel="stylesheet" type="text/css" />
<link href="css/common.css" rel="stylesheet" type="text/css" />
<link href="css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-3.6.0.min.js"></script>
<script type="text/javascript" src="js/axios.min.js"></script>
<title>登陆</title>
</head>

<body>
<!--顶部样式-->
<div class="top_header">
<em class="left_img"></em>
<div class="header clearfix" id="header">
<div class="header_Section">
</div>
</div>
<em class="right_img"></em>
</div>
<!---->
<div class="Inside_pages clearfix">
<!--登录样式-->
  <div class="login">
       <div class="style_login clearfix">
      <form id="loginForm">
          <div class="layout">
            <div class="login_title">登录</div>
            <div class="item item-fore1"><label for="loginname" class="login-label name-label"></label><input name="phone" id="phone" type="text" class="text" placeholder="请输入手机号"></div>
            <div class="item item-fore2"><label for="nloginpwd" class="login-label pwd-label"></label><input name="password" id="password" type="password" class="text" placeholder="用户密码"> </div>
            <div class="auto-login"><label class="auto-label"><input type="checkbox" id="rememberMe"><span>记住账号和密码</span></label></div>
            <div class="login-btn"><button class="btn_login" type="submit">登&nbsp;&nbsp;&nbsp;&nbsp;录</button></div>
            <div class="login_link"><a href="registered.html">免费注册</a> | <a href="#">忘记密码</a></div>
          </div>
          </form>
       </div>
       <div class="login_img"><img src="images/login_img_03.png" /></div>
  </div>
</div>
<script>
           $(document).ready(function() {
               $("#loginForm").on("submit", function(event) {
                   event.preventDefault(); // Prevent form submission
   
                   var phone= $("#phone").val();
                   var password = $("#password").val();
   
                   $.ajax({
                       url: "http://127.0.0.1:8080/consumer/login", 
                       type: "POST",
   					contentType: "application/json",
                        data: JSON.stringify({ phone: phone, password: password }),
                       dataType: "json",
                       success: function(response) {
                           if (response.success) {		   
                               const consumerId = response.consumerId;
								sessionStorage.setItem('consumerId',consumerId);
								sessionStorage.setItem('phone',response.phone);
								sessionStorage.setItem("token",response.token);
							window.location.href="index.html"
                           } else {
                               alert("密码错误");
                           }
                       },
                     error: function(jqXHR, textStatus, errorThrown) {
                         console.error("Error occurred:", jqXHR.responseText);
                         alert("Error occurred: " + textStatus);
                     }
                   });
               });
           });
       </script>
</body>
</html>
